﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;

namespace MS.INV.SysLst
{
    public class L_Ban : IDisposable
    {
        DataLayer m_DataLayer;

        #region Prop
        public System.Int32 IdBan
        {
            get;
            set;
        }

        public System.String TenBan
        {
            get;
            set;
        }

        public System.Int32 IdViTri
        {
            get;
            set;
        }

        public System.String GhiChu
        {
            get;
            set;
        }

        public System.Boolean Inactive
        {
            get;
            set;
        }

        #endregion

        public L_Ban()
        {
            m_DataLayer = new DataLayer();
        }

        public void Dispose()
        {
            try
            {
                if (this.m_DataLayer != null)
                {
                    this.m_DataLayer.Dispose();
                }
                this.m_DataLayer = null;
            }
            catch
            {
            }
        }

        public void ResetData()
        {
            IdBan = 0;
            TenBan = string.Empty;
            IdViTri = 0;
            GhiChu = string.Empty;
            Inactive = false;
        }

        public void LoadData(System.Int32 v_IdBan)
        {
            DataTable dt = new DataTable();
            try
            {
                dt = m_DataLayer.ExecDataTable("SP_L_Ban", " Command,IdBan", "LoadData", v_IdBan);
                this.ResetData();
                if (dt == null || dt.Rows.Count <= 0)
                    return;
                this.FillData(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void FillData(DataRow row)
        {
            ResetData();
            if (row.Table.Columns.Contains("IdBan"))
                IdBan = Lib.Data.ValueType.GetValue(row["IdBan"], IdBan);
            if (row.Table.Columns.Contains("TenBan"))
                TenBan = Lib.Data.ValueType.GetValue(row["TenBan"], TenBan);
            if (row.Table.Columns.Contains("IdViTri"))
                IdViTri = Lib.Data.ValueType.GetValue(row["IdViTri"], IdViTri);
            if (row.Table.Columns.Contains("GhiChu"))
                GhiChu = Lib.Data.ValueType.GetValue(row["GhiChu"], GhiChu);
            if (row.Table.Columns.Contains("Inactive"))
                Inactive = Lib.Data.ValueType.GetValue(row["Inactive"], Inactive);
        }

        public DataTable GetDataByPosId(int v_Pos)
        {
            return m_DataLayer.ExecDataTable("Sp_L_Ban", "Command,IdViTri", "GetDataByPosId", v_Pos);
        }

        public DataTable GetAll()
        {
            return m_DataLayer.ExecDataTable("Sp_L_Ban", "Command", "GetAll");
        }

        public DataTable GetAllBusyTable()
        {
            return m_DataLayer.ExecDataTable("Sp_L_Ban", "Command", "GetAllBusyTable");
        }

        public DataTable ListWithLocation()
        {
            return m_DataLayer.ExecDataTable("Sp_L_Ban", "Command", "ListWithLocation");
        }

        public void Update(ref System.Data.DataTable Data)
        {
            try
            {
                Hashtable output = null;
                m_DataLayer.BeginTransaction();

                foreach (DataRow row in Data.Rows)
                {
                    //if (row.RowState == DataRowState.Added || row.RowState == DataRowState.Modified)
                    //{
                        m_DataLayer.ExecNonQuery("Sp_L_Ban", "Command,IdBan,TenBan,IdViTri,GhiChu,Inactive", ref output, "Update",
                            Cvert.ToInt32(row["IdBan"], 0), Cvert.ToString(row["TenBan"], ""),
                            Cvert.ToInt32(row["IdViTri"], 0), Cvert.ToString(row["GhiChu"], ""),
                            Cvert.ToBoolean(row["Inactive"], false));

                        if (!object.Equals(output, null) && output.Count > 0)
                            row["IdBan"] = Cvert.ToInt32(output["IdBan"], 0);
                    //}
                }

                m_DataLayer.CommitTransaction();
            }
            catch(Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        public void DeleteRow(DataRow Row)
        {
            m_DataLayer.ExecNonQuery("Sp_L_Ban", "Command,IdBan", "DeleteRow", Cvert.ToInt32(Row["IdBan"], 0));
        }
    }
}
